#include "stdafx.h"
#include "CLimits.h"

int	CLimits::s_nMaxScriptSpace		= 0;
int	CLimits::s_nNumVars				= 0;
int	CLimits::s_nNumModulesVars		= 0;
int	CLimits::s_nNumScriptVars		= 0;
int	CLimits::s_nNumScriptTimers		= 0;
int	CLimits::s_nMaxArraySize			= 0;
int	CLimits::s_nMaxStringLength		= 0;
int	CLimits::s_nMaxTextLabelLength	= 0;
int	CLimits::s_nMaxModels			= 0;

const sLimit CLimits::s_pLimits[] =
{
	{"MODELS",						&s_nMaxModels},
	{"SCRIPT_SPACE",					&s_nMaxScriptSpace},
	{"NUM_SCRIPT_VARS",				&s_nNumVars},
	{"NUM_MODULE_LVARS",				&s_nNumModulesVars},
	{"NUM_SCRIPT_LVARS",				&s_nNumScriptVars},
	{"NUM_SCRIPT_TIMERS",			&s_nNumScriptTimers},
	{"JUMP_TABLE_SIZE",				&s_nJumpTableSize},
	{"MAX_ARRAY_SIZE",				&s_nMaxArraySize},
	{"MAX_STRING_LENGTH",			&s_nMaxStringLength},
	{"MAX_TEXT_LABEL_LENGTH",		&s_nMaxTextLabelLength},
};

void CLimits::LoadFromFile(const char *szFileName)
{
	if(FILE *hFile = OpenFile(szFileName, "rt"))
	{
		while(char *str = GetConfigLine(hFile))
		{
			char name[128], szVal[32];
			if(sscanf(str, "%s %s", name, szVal) >= 2)
			{
				int value = StringToInt(szVal);
				for(int i=0; i<sizeof(s_pLimits); i++)
				{
					if(!strcmp(name, s_pLimits[i].m_szName))
					{
						*s_pLimits[i].m_pValue = value;
						break;
					}
				}
			}
		}
		CloseFile(hFile);
	}
	else throw(SCRambl.BuildText(SCRambl.GetText("GENERAL", "OERR"), NULL, 0, "%s", szFileName));
}